/**
* 项目名称：tv   
* 类名称：TVHtmlParser   
* 类描述：   
* 创建人：Issac   
* 创建时间：2011-7-10 上午11:53:49   
* 修改人：Issac   
* 修改时间：2011-7-10 上午11:53:49   
* 修改备注：   
* @version 
 */
package com.tv.htmlparser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.RemarkNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TitleTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.HtmlPage;

import com.tv.tools.Configuration;

/**
 * @ClassName: TVHtmlParser
 * @Description: TODO
 * @author issac2009@vip.qq.com
 * @date 2011-7-10 上午11:53:49
 */
public class TVHtmlParser {
	
	/**
     * 按页面方式处理.解析标准的html页面 
     * @param content 网页的内容 
     * @throws Exception 
     */  
    public static void readByHtml(String content) throws Exception {  
        Parser myParser;  
        myParser = Parser.createParser(content, "gb2312");  
        HtmlPage visitor = new HtmlPage(myParser);  
        myParser.visitAllNodesWith(visitor);  
  
        String textInPage = visitor.getTitle();  
        //System.out.println(textInPage);  
        NodeList nodelist;  
        nodelist = visitor.getBody();  
          
       // System.out.print(nodelist.asString().trim());  
    } 
    
    /** 
     * 分别读纯文本和链接. 
     * @param result 网页的内容 
     * @throws Exception 
     */  
    public static void readTextAndLinkAndTitle(String result) throws Exception {  
        Parser parser;  
        NodeList nodelist;  
        parser = Parser.createParser(result, "gb2312");  
        
        
        // 取div id为datalist的，属性为ul的值
        NodeFilter contentFilter = new AndFilter(new TagNameFilter("ul"), 
    			new HasParentFilter(new AndFilter(
    					new TagNameFilter("div"), new HasAttributeFilter("id", "datalist"))));

        
        //NodeFilter divFilter = new NodeClassFilter(Div.class);  
        //OrFilter lastFilter = new OrFilter();  
        //lastFilter.setPredicates(new NodeFilter[] {divFilter});  
        
        nodelist = parser.parse(contentFilter);  
        Node[] nodes = nodelist.toNodeArray();  
        String line = "";  
          
        for (int i = 0; i < nodes.length; i++) {  
            Node node = nodes[i];  
            if (node instanceof Tag) {  
            	Tag tag = (Tag) node;  
            	
            	NodeList liList = tag.getChildren();
            	Node[] liNodes = liList.toNodeArray();  
            	
            	for (int j = 0; j < liNodes.length; j++){
            		Node liNode = liNodes[i];  
            		if (liNode instanceof TextNode){
            			System.out.println(liNode.getText());
            		}
            		
            	}
            	
                
            }        
            
            if (isTrimEmpty(line))  
                continue;  
            System.out.println(line);  
        }  
    }  
    
    /**
     * 去掉左右空格后字符串是否为空 
     */  
    public static boolean isTrimEmpty(String astr) {  
        if ((null == astr) || (astr.length() == 0)) {  
            return true;  
        }  
        if (isBlank(astr.trim())) {  
            return true;  
        }  
        return false;  
    }
    
    /** 
     * 字符串是否为空:null或者长度为0. 
     */  
    public static boolean isBlank(String astr) {  
        if ((null == astr) || (astr.length() == 0)) {  
            return true;  
        } else {  
            return false;  
        }  
    } 

	/**
	 * @Title: main
	 * @Description: TODO
	 * @param @param args    
	 * @return void  
	 * @throws
	 */
	public static void main(String[] args) {
		//read path
		String fileStr = Class.class.getClass().getResource("/").getPath();
		Configuration rc = new Configuration(fileStr + "init.properties");
		String path = rc.getValue("website.src") + "v.163.com_tvlist_index_27.html_tid=36.html";
		
		File file = new File(path);
		BufferedReader reader = null;
		String data = null;  
		String content = null;
		
		
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "gb2312");
			reader = new BufferedReader(isr);
			
	        while((data = reader.readLine())!=null){  
	            //System.out.println(data);  
	            content += data;
	        }
	        reader.close();
	        
			readByHtml(content);
			readTextAndLinkAndTitle(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if (reader != null) {
	            try {
	                reader.close();
	            } 
	            catch (IOException e1) {
	            }
			}
		}
	}

}
